let page = 0;
let loading = false;

function load() {
    if (loading) return;
    loading = true;
    fetch(`/api/products/featured?page=${page}&size=6`)
        .then(r => r.json())
        .then(data => {
            const box = document.getElementById('featured-container');
            data.content.forEach(p => {
                const div = document.createElement('div');
                div.className = 'card';
                div.innerHTML = `
                    <img src="${p.imageUrl}" alt=""/>
                    <h3>${p.name}</h3>
                    <p>${p.description}</p>
                `;
                box.appendChild(div);
            });
            page++;
            loading = false;
        });
}

const observer = new IntersectionObserver(() => load(), { threshold: 1.0 });
observer.observe(document.getElementById('loader'));
// 首次加载
load();